/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.DAO;

import Model.Beans.Student;
import Model.Beans.User;
import Model.Utilities.Constantes.StudentState;
import Model.Utilities.Constantes.TypeUser;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Eduardo
 */
public class UserDAO extends AbstracDAO<User> {
    
    public List<User> searchStudentsPend(){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class);
        criteria.setFetchMode("students", FetchMode.JOIN);
        criteria.add(Restrictions.eq("type", TypeUser.student));
        criteria.add(Restrictions.eq("state", StudentState.PENDIENTE));
        List<User> result= criteria.list();
        session.close();
        return result;
    }
    
    
    public User searchUserByStudent(int idStudent){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Student.class);
        criteria.setFetchMode("user", FetchMode.JOIN);
        criteria.add(Restrictions.eq("idstudent", idStudent));
        Student student = (Student)criteria.uniqueResult();
        Criteria criteria_2 = session.createCriteria(User.class);
        criteria_2.add(Restrictions.eq("iduser", student.getUser().getIduser()));
        User result = (User) criteria_2.uniqueResult();
        session.close();
        return result;        
    }
}
